package com.h.aop.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class AspectDemo1 {
    /**
     * @Pointcut()
     * 封装切点表达式，引用这个切点表达式的是时候就直接调用这个方法
     */
    @Pointcut("execution(* com.h.aop.controller.*.*(..))")
    private void pt(){}

    /**
     * 只有 Around 存在执行目标方法
     * @param pjp
     * @return
     */
    @Around("pt()")
    public Object doAround(ProceedingJoinPoint pjp) {
        log.info("Around环绕通知开始.....");

        Object proceed = null;
        try {
            proceed = pjp.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }

        log.info("Around环绕通知结束.....");

        return proceed;
    }

    @Before("pt()")
    public void doBefore() {
        log.info("Before前置通知.......");
    }

    @After("pt()")
    public void doAfter() {
        log.info("After后置通知.......");
    }

    @AfterReturning("pt()")
    public void doAfterReturning() {
        log.info("AfterReturning返回后通知.......");
    }

    @AfterThrowing("pt()")
    public void doAfterThrowing() {
        log.info("AfterThrowing异常后通知.......");
    }
}
